<?php 
	if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
	class qltk_admin_controller extends CI_Controller {
		public function __construct(){    
			parent::__construct();
			$this->load->helper("url");    
		}
		function index()
		{
			session_start();
			$this->load->library('util');
			$this->load->library('firephp');
			
			if($this->util->checkLoginAdmin()==false) {
				return;
			}
			
			$data['bar'] = $this->load->view('bar_view_admin',null,true);
			$data['sidemenu'] = $this->load->view('sidemenu_admin_view',null,true);
			
			$this->util->connect();
			$this->load->model('taikhoan_model');

			$data['taikhoan'] = $this->taikhoan_model->getTaiKhoan();
			$tk = $this->taikhoan_model->getTaiKhoan();
			
			$this->load->model('sanpham_model');
			$data['sosp'] = $this->sanpham_model->getSoSP($tk);
			
			$this->load->model('binhluan_model');
			$data['sobl'] = $this->binhluan_model->getSoBL($tk);
			
			$this->load->view('qltk_admin_view.php', $data);

		}
		
		function xoataikhoan(){
			session_start();
			$this->load->library('util');
			
			if($this->util->checkLoginAdmin()==false) {
				return;
			}
			
			$data['bar'] = $this->load->view('bar_view_admin',null,true);
			$data['sidemenu'] = $this->load->view('sidemenu_admin_view',null,true);
			
			$this->util->connect();
			$this->load->model('taikhoan_model');
			
			$matk = $_POST['matk'];
			
			$this->taikhoan_model->xoataikhoan($matk);		
			$data['taikhoan'] = $this->taikhoan_model->getTaiKhoan();
			$tk = $this->taikhoan_model->getTaiKhoan();
			
			$this->load->model('sanpham_model');
			$data['sosp'] = $this->sanpham_model->getSoSP($tk);
			
			$this->load->view('qltk_admin_view_ajax.php', $data);
		}
		
		public function dangnhap_admin()
		{
			session_start();
			$this->load->library('util');
			//$this->output->enable_profiler(TRUE);
			if(!isset($_SESSION['tenadmin'])){
				if($this->input->post(username)){
					$u = $this->input->post(username);
					$has_pw = $this->input->post(password);
					$pw = sha1($has_pw);
					$this->load->model('taikhoan_model');
					$this->taikhoan_model->verifyUser($u,$pw);
					if(isset($_SESSION['tenadmin'])){
						redirect(base_url().'index.php/qltk_admin_controller');
					}else{
						$data['error'] = $_SESSION['error'];
						$data['username'] = $u;
					}
				}
				$this->load->view('dangnhap_admin_view', $data);	
			}else{
				redirect(base_url().'index.php/qltk_admin_controller');		
			}
		}
		
		public function dangxuat_admin()
		{
			session_start();
			if(isset($_SESSION['tenadmin'])){
				session_destroy();
				$data['error'] = 'Bạn vừa đăng xuất!';
	
			}
			$this->load->view('dangnhap_admin_view', $data);				
		}
	}